package Plug.operLog;


import Plug.result.RetResult;

import Server.entity.RequestLog;
import Server.entity.Users;
import Server.service.LogService;
import Server.tool.BeanToJson;
import com.alibaba.fastjson.JSON;
import net.sf.json.JSONObject;
import org.apache.shiro.SecurityUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.util.Enumeration;

@Aspect
@Component
public class SysLogAspect {
    @Autowired
    private LogService logService;

    //Controller层切点
    @Pointcut("@annotation(Plug.operLog.OperLog)")
    public void controllerAspect(){
    }

    @Before("controllerAspect()")
    public void doBefore(JoinPoint joinPoint){
        System.out.println("调用前");
    }


    //切面 配置通知
    @AfterReturning(value = "controllerAspect()",returning = "re")
    public void saveSysLog(JoinPoint joinPoint, RetResult re) throws Exception {
        System.out.println("调用后");
        //保存日志

        //从切面织入点处通过反射机制获取织入点处的方法
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        //获取切入点所在的方法
        Method method = signature.getMethod();
        String value="";
        //获取操作
        OperLog myLog = method.getAnnotation(OperLog.class);
        if (myLog != null) {
            value= myLog.value();
        }
        //请求的参数
        Object[] args = joinPoint.getArgs();
        String params="";
        //将参数所在的数组转换成json
        for(Object obj:args){
            if(obj!=null){
                params+=JSON.toJSONString(obj.toString());
            }
        }

        //获取用户ip地址
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();

        Enumeration er = request.getHeaderNames();//获取请求头的所有name值
        StringBuilder herder = new StringBuilder();
        while (er.hasMoreElements()) {
            String name = (String) er.nextElement();
            String values = request.getHeader(name);
            herder.append("  " + name + "=" + values);
        }
        RequestLog sysLog = new RequestLog();
        sysLog.setApiName(value);
        sysLog.setJsJson(params);
        sysLog.setHeaders(herder.toString());
        sysLog.setIp(request.getRemoteHost());

        sysLog.setFhJson(BeanToJson.toJsonObjectString(re));
        JSONObject jsonObject = JSONObject.fromObject(re);
        if(jsonObject.getInt("code")==200){
            sysLog.setStatus(RequestLog.Status.YES);
        }else{
            sysLog.setStatus(RequestLog.Status.NO);
        }

        Users fspuser = (Users) SecurityUtils.getSubject().getSession().getAttribute("Users");
        sysLog.setAuthor(fspuser);
        logService.logSave(sysLog);
    }

}
